home *** CD-ROM | disk | FTP | other *** search
/ Nebula 2 / Nebula Two.iso / Apps / ScreenSavers / BackSpaceViews / SpiralView.BackModule / Source / SpiralView.m < prev    next >
Encoding:
Text File  |  1996-02-01  |  5.9 KB  |  276 lines

  1.  
  2. #import <appkit/appkit.h>
  3. #import <dpsclient/wraps.h>
  4. #import <libc.h>
  5. #import <math.h>
  6.  
  7. #import "SpiralView.h"
  8. #import "SpiralWraps.h"
  9. #import "alphanum.h"
  10. #import "Thinker.h"
  11.  
  12. #define MAXCOLORS 14
  13. static struct color {
  14.     float r, g, b;
  15. } colorChanges[MAXCOLORS] = {
  16.     /* color gradient */
  17.     {1.0, 1.0, 1.0},    /* white */
  18.     {1.0, 0.6, 1.0},    /* pink */
  19.     {1.0, 0.1, 1.0},    /* fuschia */
  20.     {0.4, 0.0, 1.0},    /* purple */
  21.     {0.0, 0.0, 1.0},    /* blue */
  22.     {0.0, 0.3, 1.0},    /* blue cyan */
  23.     {0.0, 1.0, 1.0},    /* cyan */
  24.     {0.0, 1.0, 0.0},    /* green */
  25.     {0.7, 1.0, 0.0},    /* yellow green */
  26.     {1.0, 1.0, 0.0},    /* yellow */
  27.     {1.0, 0.7, 0.0},    /* yellow orange */
  28.     {1.0, 0.3, 0.0},    /* orange */
  29.     {1.0,  0.0, 0.0},    /* red */
  30.     {0.75, 0.0, 0.0},    /* dark red */
  31. };
  32.  
  33. // The values MUST be unique, else all array elements are pointers
  34. //        to the exact same value!
  35. static NXDefaultsVector    spiralDefaults = {
  36.     {"SpiralMaxCount", "0000"}
  37.     ,{"SpiralMinRadius", "0001"}
  38.     ,{"SpiralMaxRadius", "0002"}
  39.     ,{NULL}
  40. };
  41.  
  42.  
  43. @implementation SpiralView
  44.  
  45. - initFrame:(const NXRect *)frameRect
  46. {
  47.     [super initFrame:frameRect];
  48.  
  49.     // initialize variables (many will be init via startOver)
  50.     degCircles = 3;
  51.     colorIndex = 0;
  52.     [self loadDefaults];
  53.  
  54.     [self startOver];
  55.  
  56.     return self;
  57. }
  58.  
  59. //**********
  60. - oneStep
  61. {
  62.     if (numSpirals >= maxSpirals) {
  63.         [self startOver];
  64.         [self display];
  65.         numSpirals = 0;
  66.     }
  67.  
  68.     // draw new arc
  69.     PSLSetColor (colorChanges[colorIndex].r - (degree/maxDegree), colorChanges[colorIndex].g - (degree/maxDegree), colorChanges[colorIndex].b - (degree/maxDegree));
  70.     PSLDrawArc(x, y, radius, degree);
  71.  
  72.     radius -= (maxCurrentRadius / maxDegree);
  73.     degree += 1.0;
  74.     degIterations++;
  75.     if ((degIterations > (maxDegree)) || (radius < 1))
  76.         [self newSpiral];
  77.  
  78.     return self;
  79. }
  80.  
  81. //**********
  82. - (const char *)windowTitle
  83. {    return "Spiral Generator";
  84. }
  85.  
  86. - inspector:sender
  87. {
  88.     char buf[MAXPATHLEN];
  89.  
  90.     if (!inspectorPanel) {
  91.         sprintf(buf,"%s/Spiral.nib",[sender moduleDirectory:"Spiral"]);
  92.         [NXApp loadNibFile:buf owner:self withNames:NO];
  93.  
  94.         // validate that defaults are within UI constraints
  95.         [self constrainDefaults];
  96.  
  97.         // set UI Object values
  98.         [minRadiusSlider setIntValue:minRadius];
  99.         [minRadiusField  setIntValue:minRadius];
  100.         [maxRadiusSlider setIntValue:maxRadius];
  101.         [maxRadiusField  setIntValue:maxRadius];
  102.         [maxSpiralsSlider setIntValue:maxSpirals];
  103.         [maxSpiralsField  setIntValue:maxSpirals];
  104.     }
  105.     return inspectorPanel;
  106. }
  107.  
  108. - sizeTo:(NXCoord)width :(NXCoord)height
  109. {
  110.     [super sizeTo:width :height];
  111.     [self startOver];
  112.     return self;
  113. }
  114.  
  115. - drawSelf:(const NXRect *)rects :(int)rectCount
  116. {
  117.     if (!rects || !rectCount) return self;
  118.     
  119.     PSsetgray(NX_BLACK);
  120.     NXRectFill(rects);
  121.     return self;
  122. }
  123.  
  124. - (BOOL) useBufferedWindow;
  125. {    return YES;
  126. }
  127.  
  128. //**********
  129. - changeMinRadius:sender
  130. {
  131.     int temp;
  132.  
  133.     if (sender == minRadiusField) {
  134.         temp = [sender intValue];
  135.         if (temp < [minRadiusSlider minValue])
  136.             temp = minRadius;
  137.         if (temp > [minRadiusSlider maxValue])
  138.             temp = minRadius;
  139.         [sender setIntValue:temp];
  140.     }
  141.  
  142.     minRadius = [sender floatValue];
  143.     if (minRadius > maxRadius) {
  144.         minRadius = maxRadius;
  145.         [minRadiusSlider setIntValue:minRadius];
  146.     }
  147.     [minRadiusField setIntValue:minRadius];
  148.     [minRadiusSlider setIntValue:minRadius];
  149.  
  150.     [self saveDefaults];
  151.     return self;
  152. }
  153.  
  154. - changeMaxRadius:sender
  155. {
  156.     int temp;
  157.  
  158.     if (sender == maxRadiusField) {
  159.         temp = [sender intValue];
  160.         if (temp < [maxRadiusSlider minValue])
  161.             temp = maxRadius;
  162.         if (temp > [maxRadiusSlider maxValue])
  163.             temp = maxRadius;
  164.         [sender setIntValue:temp];
  165.     }
  166.  
  167.     maxRadius = [sender floatValue];
  168.     if (minRadius > maxRadius) {
  169.         maxRadius = minRadius;
  170.         [maxRadiusSlider setIntValue:maxRadius];
  171.     }
  172.     [maxRadiusField setIntValue:maxRadius];
  173.     [maxRadiusSlider setIntValue:maxRadius];
  174.  
  175.     [self saveDefaults];
  176.     return self;
  177. }
  178.  
  179. - changeMaxSpirals:sender
  180. {
  181.     int temp;
  182.  
  183.     if (sender == maxSpiralsField) {
  184.         temp = [sender intValue];
  185.         if (temp < [maxSpiralsSlider minValue])
  186.             temp = [maxSpiralsSlider minValue];
  187.         if (temp > [maxSpiralsSlider maxValue])
  188.             temp = [maxSpiralsSlider maxValue];
  189.         [sender setIntValue:temp];
  190.     }
  191.  
  192.     maxSpirals = [sender floatValue];
  193.     [maxSpiralsField setIntValue:maxSpirals];
  194.     [maxSpiralsSlider setIntValue:maxSpirals];
  195.  
  196.     [self saveDefaults];
  197.     return self;
  198. }
  199.  
  200. //**********
  201. - (void)loadDefaults
  202. {
  203.     itoa(MAXSPIRALS, spiralDefaults[0].value);
  204.     itoa(MINRADIUS, spiralDefaults[1].value);
  205.     itoa(MAXRADIUS, spiralDefaults[2].value);
  206.     NXRegisterDefaults("BackSpace", spiralDefaults);
  207.  
  208.     maxSpirals = atoi((char*)NXGetDefaultValue("BackSpace", "SpiralMaxCount"));
  209.     minRadius = atoi((char*)NXGetDefaultValue("BackSpace", "SpiralMinRadius"));
  210.     maxRadius = atoi((char*)NXGetDefaultValue("BackSpace", "SpiralMaxRadius"));
  211. }
  212.  
  213. - (void)saveDefaults
  214. {
  215.     itoa(maxSpirals, spiralDefaults[0].value);
  216.     itoa(minRadius, spiralDefaults[1].value);
  217.     itoa(maxRadius, spiralDefaults[2].value);
  218.     NXWriteDefaults("BackSpace", spiralDefaults);
  219. }
  220.  
  221. - (void)constrainDefaults
  222. {
  223.     // validate that defaults are within UI constraints
  224.     if (minRadius < [minRadiusSlider minValue])
  225.         minRadius = [minRadiusSlider minValue];
  226.     if (minRadius > [minRadiusSlider maxValue])
  227.         minRadius = [minRadiusSlider maxValue];
  228.  
  229.     if (maxRadius < [maxRadiusSlider minValue])
  230.         maxRadius = [maxRadiusSlider minValue];
  231.     if (maxRadius > [maxRadiusSlider maxValue])
  232.         maxRadius = [maxRadiusSlider maxValue];
  233.  
  234.     if (minRadius > maxRadius)
  235.         minRadius = maxRadius;
  236.  
  237.     if (maxSpirals < [maxSpiralsSlider minValue])
  238.         maxSpirals = [maxSpiralsSlider minValue];
  239.     if (maxSpirals > [maxSpiralsSlider maxValue])
  240.         maxSpirals = [maxSpiralsSlider maxValue];
  241. }
  242.  
  243. //**********
  244. - startOver
  245. {
  246.     numSpirals = 0;
  247.     maxDegree = 360 * degCircles;
  248.     [self newSpiral];
  249.  
  250.     return self;
  251. }
  252.  
  253. - newSpiral
  254. {
  255.     NXRect    rBounds;
  256.     NXSize    rSize;
  257.  
  258.     numSpirals++;
  259.     degree = 0;
  260.     degIterations = 0;
  261.  
  262.     [self getBounds:&rBounds];
  263.     rSize = rBounds.size;
  264.  
  265.     x = randBetween(0, rSize.width);
  266.     y = randBetween(0, rSize.height);
  267.     radius = randBetween(minRadius, maxRadius);
  268.     maxCurrentRadius = radius;
  269.     if (++colorIndex >= MAXCOLORS)
  270.         colorIndex = 0;
  271.  
  272.     return self;
  273. }
  274.  
  275. @end
  276.